<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

aos_header( 'Diplomacy' );
?>
					<div class="blok">
<?php
printDiplomacyTab( 'Factions' );
?> 
					</div>
					<br />
<?php
$query = "SELECT faction_id, access, countdown " .
		"FROM faction_member " .
		"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND requested = 0 AND invited = 0";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array( $result, MYSQL_ASSOC );
if( isset( $row[ 'faction_id' ] ) && $row[ 'countdown'] == 0 ) {
	$access = $row[ 'access' ];
	$faction_id = $row[ 'faction_id' ];
	$query = "SELECT count(*) FROM faction_member WHERE requested = 0 AND faction_id = " . $faction_id;
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	$members = $row[ 'count(*)' ];
	$query = "SELECT faction_id, name, type_name, leader, motd, tax_set, gold, coal, iron, food, policy_cost, " .
			"yesterday_gold_income, upkeep, faction.faction_type_id, max_members, farm_support, coal_support, iron_support, " .
			"score, war_points, economy_points, draft, alert, nationalism, control_type " .
			"FROM faction JOIN faction_types ON faction.faction_type_id = faction_types.faction_type_id WHERE faction_id = " . $faction_id;
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	$text = stripslashes( $row[ 'motd' ] );
	$policy_cost = $row[ 'policy_cost' ];
	$tax_set = $row[ 'tax_set' ];
	$control_type = $row[ 'control_type' ];
	$gold_tmp = $row[ 'gold' ];
	$iron_tmp = $row[ 'iron' ];
	$coal_tmp = $row[ 'coal' ];
	$food_tmp = $row[ 'food' ];
	$upkeep_tmp = $row[ 'upkeep' ];
	$max_members_tmp = $row[ 'max_members' ];
	$faction_type_id_tmp = $row[ 'faction_type_id' ];
	$yesterday_tmp = $row[ 'yesterday_gold_income' ];
	$faction_policy = "";
	$count = 0;
	if( $row[ 'farm_support' ] == 1 ) {
		$count ++;
		$faction_policy .= "<td>" . translate( 'support farmers' ) . "</td>";
	}
	if( $row[ 'iron_support' ] == 1 ) {
		if( $count == 1 ) {
			$faction_policy .= "<td class=\"middle\"></td>";
		}
		$count ++;
		$faction_policy .= "<td>" . translate( 'support iron miners' ) . "</td>";
	}
	if( $row[ 'coal_support' ] == 1 ) {
		if( $count == 1 ) {
			$faction_policy .= "<td class=\"middle\"></td>";
		}
		$count ++;
		$faction_policy .= "<td>" . translate( 'support coal miners' ) . "</td>";
	}
	for( $i = 0; $i < 3 - $count; $i ++ ) {
		if( $count + $i == 1 ) {
			$faction_policy .= "<td class=\"middle\"></td>";
		}
		$faction_policy .= "<td>&nbsp;</td>";
	}

	$military_policy = "";
	$count = 0;
	if( $row[ 'draft' ] == 1 ) {
		$count ++;
		$military_policy .= "<td>" . translate( 'higher draft rates' ) . "</td>";
	}
	if( $row[ 'alert' ] == 1 ) {
		if( $count == 1 ) {
			$military_policy .= "<td class=\"middle\"></td>";
		}
		$count ++;
		$military_policy .= "<td>" . translate( 'armies on high alert' ) . "</td>";
	}
	if( $row[ 'nationalism' ] == 1 ) {
		if( $count == 1 ) {
			$military_policy .= "<td class=\"middle\"></td>";
		}
		$count ++;
		$military_policy .= "<td>" . translate( 'encourage nationalism' ) . "</td>";
	}
	
	for( $i = 0; $i < 3 - $count; $i ++ ) {
		if( $count + $i == 1 ) {
			$military_policy .= "<td class=\"middle\"></td>";
		}
		$military_policy .= "<td>&nbsp;</td>";
	}
	
	//print( "test" );
	print( "<table id=\"overview\">\r\n" );
	print( "<tr><td>" . translate( $row[ 'type_name' ] ) . "</td><td><strong>" . $row[ 'name' ] . "</strong></td><td class=\"middle\"></td>\r\n" );
	if( $row[ 'leader' ] == $_SESSION[ 'ruler_id' ] ) {
		$leader2 = true;
	}
	else {
		$leader2 = false;
	}
	
	$score = $row[ 'score' ];
	$war_points = $row[ 'war_points' ];
	$economy_points = $row[ 'economy_points' ];
	
	$query = "SELECT count(*) " .
			"FROM faction " .
			"WHERE score > " . $score;
	$result2 = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	$rank = number_format( $row2[ 'count(*)' ] + 1 );

	//print( "test2" );
	$leader = $row[ 'leader' ];
	$query = "SELECT name " .
			"FROM ruler " .
			"WHERE ruler_id = " . $leader;
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	print( "<td>" . translate( 'Leader' ) . "</td><td><a href=\"../overview/ruler.php?name=" . $row[ 'name' ] . "\">" . $row[ 'name' ] . "</a></td></tr>\r\n" );
	$query2 = "SELECT name, x, y FROM village WHERE ruler_id = " . $leader . " AND start_place = 1";
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	
	if( isset( $row2[ 'x' ] ) ) {
		$faction_capital = "<a href=\"../operations/map.php?x=" . ( $row2[ 'x' ] - 20 ) . "&y=" . ( $row2[ 'y' ] - 20 ) . "\" title=\"" . translate( 'Show on map' ) . "\"><strong id=\"blue\">" . $row2[ 'name' ] . " (" .  $row2[ 'x' ] . ", " .  $row2[ 'y' ] . ")</strong></a>";		
	}
	else {
		$faction_capital = translate( 'None' );
	}
	
	print( "<tr><td>" . translate( 'Faction capital' ) . "</td><td>$faction_capital</td><td class=\"middle\">&nbsp;</td>\r\n" );
	print( "<td>" . translate( 'Members' ) . "</td><td>" . $members . "/" . $max_members_tmp . " (" . translate( 'including invites' ) . ")</td></tr>\r\n" );
	if( $control_type == 0 ) {
		$control_type_string = translate( 'Hierarchic' );
	}
	else {
		$control_type_string = translate( 'Democratic' );
	}
	print( "<tr><td>" . translate( 'Color' ) . "</td><td>" . get_faction_color( $faction_id ) . "</td><td class=\"middle\">&nbsp;</td><td>" . translate( 'Structure' ) . "</td><td>$control_type_string</td></tr>\r\n" );
	
	print( "<tr><td>&nbsp;</td><td>&nbsp;</td><td class=\"middle\">&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>\r\n" );
	print( "<tr><td><strong>" . translate( 'Economy' ) . "</strong></td><td>&nbsp;</td><td class=\"middle\">&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>\r\n" );
	print( "<tr><td>" . translate( 'Gold' ) . "</td><td align=\"right\"><strong id=\"gold\">" . number_format( $gold_tmp ) . "</strong></td><td class=\"middle\">&nbsp;</td>\r\n" );
	if( $iron_tmp != 0 ) {
		print( "<td>" . translate( 'Iron' ) . "</td><td align=\"right\"><strong id=\"iron\">" . number_format( $iron_tmp ) . "</strong></td></tr>\r\n" );
	}
	else {
		print( "<td></td><td></td></tr>\r\n" );
	}
	if( $yesterday_tmp > 0 ) {
		print( "<tr><td>" . translate( 'Yesterdays gold income' ) . "</td><td align=\"right\"><strong id=\"gold\">+" . number_format( $yesterday_tmp ) . "</strong></td><td class=\"middle\">&nbsp;</td>\r\n" );
	}
	else {
		print( "<tr><td></td><td></td><td class=\"middle\">&nbsp;</td>\r\n" );
	}
	if( $coal_tmp != 0 ) {
		print( "<td>" . translate( 'Coal' ) . "</td><td align=\"right\"><strong id=\"coal\">" . number_format( $coal_tmp ) . "</strong></td>\r\n" );
	}
	else {
		print( "<td></td><td></td></tr>\r\n" );
	}

	if( $food_tmp != 0 ) {
?>
				<tr>
					<td></td>
					<td></td>
					<td class="middle">&nbsp;</td>
					<td><?php transprint( 'Food' ); ?></td>
					<td align="right">
						<strong class="food"><?php print( number_format( $food_tmp ) ); ?></strong>
					</td>
				</tr>
<?php
	}

	if( $upkeep_tmp > 0 ) {
		print( "<tr><td>" . translate( 'Upkeep' ) . "</td>" .
				"<td align=\"right\"><strong class=\"gold\">-" . number_format( $upkeep_tmp ) . "</strong></td>" .
				"<td class=\"middle\">&nbsp;</td>\r\n" );
	}
	else {
		print( "<tr><td></td><td></td><td class=\"middle\">&nbsp;</td>\r\n" );
	}
	print( "<td>" . translate( 'Requested tax contribution' ) . "</td><td align=\"right\">$tax_set%</td></tr>\r\n" );
	
	if( $policy_cost > 0 ) {
		print( "<tr><td>" . translate( 'Policy cost per turn' ) . "</td><td align=\"right\"><strong class=\"gold\">-" . number_format( $policy_cost ) . "</strong></td><td class=\"middle\">&nbsp;</td><td></td><td></td></tr>\r\n" );
	}
	
	if( $faction_policy != "" ) {
		print( "<tr><td>" . translate( 'Economic policies' ) . ":</td>$faction_policy</tr>\r\n" );
	}

	print( "<tr><td>&nbsp;</td><td>&nbsp;</td><td class=\"middle\">&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>\r\n" );
	print( "<tr><td><strong>" . translate( 'Military' ) . "</strong></td><td>&nbsp;</td><td class=\"middle\">&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>\r\n" );
	$query2 = "SELECT SUM( scout ), SUM( army.infantry ), SUM( army.cavalry ), SUM( army.artillery ) FROM army " .
			"JOIN faction_member ON army.ruler_id = faction_member.ruler_id " .
			"WHERE faction_id = " . $faction_id . " AND invited = 0 AND requested = 0";
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	if( $row2[ 'SUM( scout )' ] > 0 ) {
		print( "<tr><td>" . translate( 'Deployed scouts' ) . "</td><td align=\"right\"><strong class=\"scouts\">" . number_format( $row2[ 'SUM( scout )' ] ) . "</strong></td><td class=\"middle\">&nbsp;</td>\r\n" );
	}
	else {
		print( "<tr><td></td><td></td><td class=\"middle\">&nbsp;</td>\r\n" );
	}
	if( $row2[ 'SUM( army.infantry )' ] > 0 ) {
		print( "<td>" . translate( 'Deployed infantry' ) . "</td><td align=\"right\"><strong class=\"infantry\">" . number_format( $row2[ 'SUM( army.infantry )' ] ) . "</strong></td></tr>\r\n" );
	}
	else {
		print( "<td></td><td></td></tr>\r\n" );
	}
	if( $row2[ 'SUM( army.cavalry )' ] > 0 ) {
		print( "<tr><td>" . translate( 'Deployed cavalry' ) . "</td><td align=\"right\"><strong class=\"cavalry\">" . number_format( $row2[ 'SUM( army.cavalry )' ] ) . "</strong></td><td class=\"middle\">&nbsp;</td>\r\n" );
	}
	else {
		print( "<tr><td></td><td></td><td class=\"middle\">&nbsp;</td>\r\n" );
	}
	if( $row2[ 'SUM( army.artillery )' ] > 0 ) {
		print( "<td>" . translate( 'Deployed artillery' ) . "</td><td align=\"right\"><strong class=\"artillery\">" . number_format( $row2[ 'SUM( army.artillery )' ] ) . "</strong></td></tr>\r\n" );
	}
	else {
		print( "<td></td><td></td></tr>\r\n" );
	}
	
	if( $military_policy != "" ) {
		print( "<tr><td>" . translate( 'Military policies' ) . ":</td>$military_policy</tr>\r\n" );
	}
	
	print( "<tr><td>&nbsp;</td><td>&nbsp;</td><td class=\"middle\">&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>\r\n" );
	print( "<tr><td><strong>" . translate( 'Score and ranking' ) . "</strong></td><td>&nbsp;</td><td class=\"middle\">&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>\r\n" );
	print( "<tr><td>" . translate( 'Score' ) . "</td><td align=\"right\">" . number_format( $score ) . "</td><td class=\"middle\">&nbsp;</td><td>" . translate( 'Rank' ) . "</td><td align=\"right\">$rank</td></tr>\r\n" );
	print( "<tr><td>" . translate( 'War points' ) . "</td><td align=\"right\">" . number_format( $war_points ) . "</td><td class=\"middle\">&nbsp;</td>\r\n" );
	print( "<td>" . translate( 'Economy points' ) . "</td><td align=\"right\">" . number_format( $economy_points ) . "</td></tr>\r\n" );
	print( "</table><br />\r\n" );
	if( $text != "" ) {
		print "<div class=\"blok\">\r\n";
		print( "<strong>" . translate( 'News and information' ) . "</strong>\r\n" );
		print( "<pre>\r\n" );
		print( wordwrap( $text, 100 ) );
		print( "</pre>\r\n" );
		print( "</div>\r\n" );
		print( "<br />\r\n" );
	}
	$query = "SELECT tax_set " .
			"FROM faction_member " .
			"WHERE faction_id = " . $faction_id . " AND ruler_id = " . $_SESSION[ 'ruler_id' ];
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	print( "<div class=\"blok\">\r\n" );
	print( translate( 'My contribution' ) . ": <form method=\"post\" action=\"action/set_contribution.php\" ><input type=\"text\" size=\"2\" name=\"tax_set\" value=\"" . $row[ 'tax_set' ] . "\" />% <input type=\"submit\" value=\"" . translate( 'set contribution' ) . "\" /></form><br />\r\n" );
?>
				<a href="member_list.php"><?php transprint( 'Member details and options' ); ?></a>
<?php	
	print( "</div><br />\r\n" );

	$query = "SELECT turn, turns_inactive FROM game";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	$turns_to_inactive = $row[ 'turns_inactive' ];
	$current_turn = $row[ 'turn' ];

	$query = "SELECT ruler.ruler_id, countdown, title, member_time, gold_paid, tax_set, active_turn, resigned, ban_turn, user_id, score, last_year_score, name " .
			"FROM faction_member " .
			"JOIN ruler ON ruler.ruler_id = faction_member.ruler_id " .
			"WHERE faction_id = $faction_id AND requested = 0 AND invited = 0 " .
			"ORDER BY score DESC";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$num = mysql_numrows( $result );
	$i = 0;
	print( "<table><th colspan=\"10\">" . translate( 'Members' ) . "</th>\r\n" .
			"<tr><td align=\"right\">" . translate( 'Rank' ) . "</td>" .
				"<td align=\"right\">" . translate( 'Score' ) . "</td>" .
				"<td align=\"right\">" . translate( 'Growth' ) . "</td>" .
				"<td>" . translate( 'Title' ) . "</td>" .
				"<td>" . translate( 'Name' ) . "</td>" .
				"<td align=\"right\">" . translate( 'Villages' ) . "</td>" .
				"<td align=\"right\">" . translate( 'Contribution' ) . "</td>" .
				"<td align=\"right\">" . translate( 'Per turn' ) . "</td>" .
				"<td align=\"right\">" . translate( 'Membership' ) . "</td></tr>\r\n" );
	while ( $i < $num ) {
	    if( $i % 2 == 0 ) {
	        print "<tr id=\"even\">";
	    }
	    else {
	        print "<tr>";
	    }
		$ruler_name = mysql_result( $result, $i, "name" );
		$score = mysql_result( $result, $i, "score" );
		$last_year_score = mysql_result( $result, $i, "last_year_score" );
		if( $last_year_score > 0 && $score > 0 ) {
			$growth = round( ( $score / $last_year_score - 1.0 ) * 100.0, 2 ) . "%";
		}
		else {
			$growth = '-';
		}
		
		$query2 = "SELECT count(*) " .
				"FROM village " .
				"WHERE ruler_id = " . mysql_result( $result, $i, "ruler_id" );
		$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
		$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
		$villages = $row2[ 'count(*)' ];
		
				
		$query2 = "SELECT count(*) FROM ruler WHERE score > $score";
		$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
		$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
		print( "<td align=\"right\">" . number_format( $row2[ 'count(*)' ] + 1 ) . "</td>" .
				"<td align=\"right\">" . number_format( $score ) . "</td>" .
				"<td align=\"right\">$growth</td>" );
		print( "<td>" . stripslashes( mysql_result( $result, $i, "title" ) ) . "</td>" );
		$member_time = number_format( mysql_result( $result, $i, "member_time" ) );
		if( mysql_result( $result, $i, "member_time" ) == 1 ) {
			$member_time = $member_time . " " . translate( 'day' );
		}
		else {
			$member_time = $member_time . " " . translate( 'days' );
		}
		
		$query2 = "SELECT banned FROM users WHERE user_id = " . mysql_result( $result, $i, "user_id" );
		$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
		$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
		if( mysql_result( $result, $i, "ban_turn" ) > $current_turn || ( isset( $row2[ "banned" ] ) &&  $row2[ "banned" ] == 1 ) ) {
			$inactive = " (" . translate( 'banned' ) . ")";
		}
		else if( mysql_result( $result, $i, "resigned" ) == 1 ) {
			$inactive = " (" . translate( 'resigned' ) . ")";
		}
		else if( mysql_result( $result, $i, "active_turn" ) + $turns_to_inactive < $current_turn ) {
			$inactive = " (" . translate( 'inactive' ) . ")";
		}
		else {
			$inactive = "";
		}
		
		if( $current_turn == mysql_result( $result, $i, "active_turn" ) ) {
			$online = "<strong>";
			$online_after = "</strong>";
		}
		else {
			$online = "";
			$online_after = "";
		}

		print( "<td><a href=\"../overview/ruler.php?name=" . $ruler_name . "\">$online$ruler_name$online_after</a>$inactive</td>" );
		print( "<td align=\"right\">$villages</td>" );
		print( "<td align=\"right\">" . number_format( mysql_result( $result, $i, "tax_set" ) ) . "%</td>" );
		print( "<td align=\"right\"><strong id=\"gold\">" . number_format( mysql_result( $result, $i, "gold_paid" ) ) . "</strong></td>" );
		if( mysql_result( $result, $i, "countdown" ) > 0 ) {
			print( "<td>" . mysql_result( $result, $i, "countdown" ) . " " . translate( 'days left' ) . "</td>" );
		}
		else {
			print( "<td align=\"right\">$member_time</td>" );
		} 
		print( "</tr>\r\n" );
		$i ++;
	}
	print( "</table>\r\n" );
}
elseif( $row[ 'countdown'] > 0 ) {
	print "<div class=\"blok\">\r\n";
	print( translate( 'Leaving faction' ) . ", " . $row[ 'countdown'] . " " . translate( 'days left' ) . ".\r\n" );
	print( "</div>\r\n" );
}
else {
	if( isset( $_SESSION[ 'join_faction_status' ] ) ) {
		print translate( 'Status' ) . ": " . $_SESSION[ 'join_faction_status' ] . "<br /><br />\r\n";
		unset( $_SESSION[ 'join_faction_status' ] );
	}
	$query = "SELECT DISTINCT name, type_name, leader, faction.faction_id, score, recruitment " .
			"FROM faction " .
			"JOIN faction_types ON faction_types.faction_type_id = faction.faction_type_id " .
			"ORDER BY score DESC";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$num = mysql_numrows( $result );
	
	if( $num > 0 ) {
		$i = 0;
		$even = 0;
		print "<table><th colspan=\"7\">" . translate( 'Join a faction' ) . ":</th>\r\n";
		print( "<tr><td>" . translate( 'Rank' ) . "</td><td>" . translate( 'Score' ) . "</td><td>" . translate( 'Members' ) . "</td><td>" . translate( 'Faction' ) . "</td><td>" . translate( 'Leader' ) . "</td><td>" . translate( 'Recruitment message' ) . "</td><td>" . translate( 'Request to join' ) . "</td></tr>\r\n" );
		while ( $i < $num ) {
		    if( $even % 2 == 0 ) {
		        print "<tr id=\"even\">";
		    }
		    else {
		        print "<tr>";
		    }
		    
			$query2 = "SELECT count(*) " .
					"FROM faction_member " .
					"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND faction_id = " . mysql_result( $result, $i, "faction_id" ) . " AND invited = 1";
			$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
			$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
			if( $row2[ 'count(*)' ] == 0 ) {
				$query2 = "SELECT count(*) FROM faction WHERE score > " . mysql_result( $result, $i, "score" );
				$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
				$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
				print( "<td>" . ( $row2[ 'count(*)' ] + 1 ) . "</td>" );
				print( "<td>" . number_format( mysql_result( $result, $i, "score" ) ) . "</td>" );
				$query2 = "SELECT count(*) " .
						"FROM faction_member " .
						"WHERE faction_id = " . mysql_result( $result, $i, "faction_id" ) . " AND requested = 0 AND invited = 0";
				$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
				$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
				print( "<td>" . $row2[ 'count(*)' ] . "</td>" );
				print( "<td>" . translate( mysql_result( $result, $i, "type_name" ) ) . ": " . "<a href=\"../overview/faction.php?name=" . mysql_result( $result, $i, "name" ) . "\">" . mysql_result( $result, $i, "name" ) . "</a></td>" );
				$query2 = "SELECT count(*) FROM faction_member WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND faction_id = " . mysql_result( $result, $i, "faction_id" ) . " AND requested = 1";
				$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
				$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
				
				if( $row2[ 'count(*)' ] > 0 ) {
					$request_text = translate( 'Revoke join request' );
				}
				else {
					$request_text = translate( 'Request to join' );
				}
				
				$query2 = "SELECT name " .
						"FROM ruler " .
						"WHERE ruler_id = " . mysql_result( $result, $i, "leader" );
				$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
				$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
				
				print( "<td><a href=\"../overview/ruler.php?name=" . $row2[ 'name' ] . "\">" . $row2[ 'name' ] . "</a></td><td>" . mysql_result( $result, $i, "recruitment" ) . "</td>" );
				
				$query3 = "SELECT village_id " .
						"FROM village " .
						"WHERE ruler_id = " . mysql_result( $result, $i, "leader" ) . " AND start_place = 1";
				$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
				$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
				
				if( isset( $row3[ 'village_id' ] ) && checkRangeVillage( $row3[ 'village_id' ] ) ) {
					print( "<td><form method=\"post\" action=\"action/join_request.php\"><input type=\"hidden\" value=\"" . mysql_result( $result, $i, "faction_id" ) . "\" name=\"faction_id\" /><input type=\"submit\" value=\"$request_text\"/></form></td></tr>\r\n" );
				}
				elseif( !isset( $row3[ 'village_id' ] ) ) {
					print( "<td>" . translate( 'Unable to join' ) . ". <small>(" . translate( 'The faction leader has lost his capital' ) . ")</small></td></tr>\r\n" );
				}
				else {
					print( "<td>" . translate( 'Capital too far away' ) . ". <small>(" . translate( 'Improve your communications' ) . ")</small></td></tr>\r\n" );
				}
				$even ++;
			}
		    
			$i ++;
		}
		print( "</table><br />\r\n" );
	}
	else {
 		print( "<div class=\"blok\">\r\n" );
		print( translate( 'There are no factions to join.' ) . "<br />\r\n" );
		print( "</div><br />\r\n" );    
	}
	
	$query = "SELECT name, type_name, leader, faction.faction_id, score, recruitment " .
			"FROM faction " .
			"JOIN faction_types ON faction.faction_type_id = faction_types.faction_type_id " .
			"JOIN faction_member ON faction.faction_id = faction_member.faction_id " .
			"WHERE faction_member.ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND faction_member.invited = 1 ORDER BY score DESC";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$num = mysql_numrows( $result );
	$i = 0;
	while ( $i < $num ) {
		if( $i == 0 ) {
			print "<table><th colspan=\"6\">" . translate( 'Faction invites' ) . "</th>\r\n";
			print( "<tr><td>" . translate( 'Rank' ) . "</td><td>" . translate( 'Score' ) . "</td><td>" . translate( 'Faction' ) . "</td><td>" . translate( 'Leader' ) . "</td><td>" . translate( 'Recruitment message' ) . "</td><td>" . translate( 'Request to join' ) . "</td></tr>\r\n" );
		}
		
	    if( $i % 2 == 0 ) {
	        print "<tr id=\"even\">";
	    }
	    else {
	        print "<tr>";
	    }
		$query2 = "SELECT count(*) FROM faction WHERE score > " . mysql_result( $result, $i, "score" );
		$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
		$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
		print( "<td>" . ( $row2[ 'count(*)' ] + 1 ) . "</td><td>" . number_format( mysql_result( $result, $i, "score" ) ) . "</td><td>" . mysql_result( $result, $i, "type_name" ) . ": " . mysql_result( $result, $i, "name" ) . "</td>" );
		$query2 = "SELECT name FROM ruler WHERE ruler_id = " . mysql_result( $result, $i, "leader" );
		$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
		$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
		
		print( "<td><a href=\"../overview/ruler.php?name=" . $row2[ 'name' ] . "\">" . $row2[ 'name' ] . "</a></td><td>" . mysql_result( $result, $i, "recruitment" ) . "</td>" );
		print( "<td><form method=\"post\" action=\"action/join_request.php\"><input type=\"hidden\" value=\"" . mysql_result( $result, $i, "faction_id" ) . "\" name=\"faction_id\" /><input type=\"submit\" value=\"" . translate( 'join' ) . "\"/></form></td></tr>\r\n" );
		$i ++;
		if( $i == $num ) { 
			print( "</table><br />\r\n" );
		}
	}
	
	if( isset( $_SESSION[ 'create_faction_status' ] ) ) {
		print translate( 'Status' ) . ": " . translate( $_SESSION[ 'create_faction_status' ] ) . "<br /><br />\r\n";
		unset( $_SESSION[ 'create_faction_status' ] );
	}

	print "<div class=\"blok\">\r\n";
	print( "<strong>" . translate( 'Create a faction' ) . "</strong><br />\r\n" );
	print( translate( 'Find other rulers to join you and lead them to world domination.' ) . "<br />\r\n" );
	print( "<br />\r\n" );
	print "<form method=\"post\" action=\"action/create_faction.php\">\r\n";
	print "<table id=\"small_blank\">"; 
	print "<tr><td>" . translate( 'Faction name' ) . ":</td><td><input type=\"text\" size=\"25\" name=\"name\" /></td></tr>\r\n";
	print "<tr><td>" . translate( 'Faction movement tag' ) . ":</td><td><input type=\"text\" size=\"5\" name=\"tag\" /></td></tr>\r\n";
	print "<tr><td>" . translate( 'Recruitment message' ) . ":</td><td><input type=\"text\" size=\"40\" name=\"recruitment\" /></td></tr></table>\r\n";
	print "<input type=\"submit\" value=\"" . translate( 'create' ) . "\"/><br />\r\n";
	print( "</form>\r\n</div>\r\n" );
}
?>
				<script type="text/javascript">
					function setType() {
						var tmp = document.getElementById( "faction_type_id" );
						var select = document.getElementById( "type_select" );
						tmp.setAttribute( "value", select.value );
					}
				</script>
<?php
aos_footer();
?>